package cn.openea.perpartner.meily.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.util.UserContext;
import cn.openea.perpartner.meily.web.entity.Role;
import cn.openea.perpartner.meily.web.entity.User;
import cn.openea.perpartner.meily.web.entity.UserRole;
import cn.openea.perpartner.meily.web.entity.dto.BtnRoleDto;
import cn.openea.perpartner.meily.web.entity.dto.UserRoleDto;
import cn.openea.perpartner.meily.web.service.IRoleService;
import cn.openea.perpartner.meily.web.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Weird077
 * @description TODO
 * @date 2022-12-23 9:13
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "分页查询")
    @PostMapping("/list")
    public TableDataInfo selectRoleList(@RequestBody Role role) {
        startPage();
        List<Role> roleList = roleService.selectRoleList(role);
        return getDataTable(roleList);
    }

    @ApiOperation(value = "新增角色")
    @PostMapping("/add")
    public AjaxResult save(@RequestBody Role role) {
        if (!roleService.checkRoleNameUnique(role)) {
            return error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        if (!roleService.checkRoleKeyUnique(role)) {
            return error("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        role.setCreateUser(UserContext.getUserId());
        role.setUpdateUser(UserContext.getUserId());
        return toAjax(roleService.insertRole(role));
    }

    @ApiOperation(value = "编辑角色")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody Role role) {
        role.setCreateUser(UserContext.getUserId());
        role.setUpdateUser(UserContext.getUserId());
        return toAjax(roleService.updateRole(role));
    }

    @ApiOperation(value = "删除角色")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody List<Long> idList) {
        return toAjax(roleService.deleteRoleByIds(idList));
    }

    @ApiOperation(value = "查询角色 -id")
    @GetMapping("/get/{roleId}")
    public AjaxResult getById(@PathVariable("roleId") Long roleId) {
        Role role = roleService.getById(roleId);
        return BeanUtil.isNotEmpty(role) ? success(role) : error("角色不存在");
    }

    @ApiOperation(value = "查询角色 -key")
    @GetMapping("/query/{roleKey}")
    public TableDataInfo getByRoleKey(@PathVariable("roleKey") String roleKey) {
        List<Role> roleList = roleService.getByRoleKey(roleKey);
        return getDataTable(roleList);
    }

    @ApiOperation(value = "查询角色 -key&userId")
    @GetMapping("/query/{roleKey}/{userId}")
    public AjaxResult getByRoleKey(@PathVariable("roleKey") String roleKey, @PathVariable("userId") String userId) {
        List<Role> roles = roleService.getByRoleKeyAndUserId(roleKey, userId);
        if(roles == null || roles.size() < 1){
            return AjaxResult.error("查询成功，没有发起业务的权限！", false);
        }
        return AjaxResult.success("查询成功，有发起业务的权限！", true);
    }

    @ApiOperation(value = "校验角色名称")
    @PostMapping("/checkRoleNameUnique")
    public boolean checkRoleNameUnique(@RequestBody Role role) {
        return roleService.checkRoleNameUnique(role);
    }

    @ApiOperation(value = "校验角色权限")
    @PostMapping("/checkRoleKeyUnique")
    public boolean checkRoleKeyUnique(@RequestBody Role role) {
        return roleService.checkRoleKeyUnique(role);
    }


    @ApiOperation(value = "已分配用户角色列表")
    @PostMapping("/authUser/allocatedList")
    public TableDataInfo allocatedList(@RequestBody UserRoleDto dto) {
        startPage();
        List<User> list = userService.selectAllocatedList(dto);
        return getDataTable(list);
    }

    @ApiOperation(value = "未分配用户角色列表")
    @PostMapping("/authUser/unallocatedList")
    public TableDataInfo unallocatedList(@RequestBody UserRoleDto dto) {
        startPage();
        List<User> list = userService.selectUnallocatedList(dto);
        return getDataTable(list);
    }

    @ApiOperation(value = "取消授权")
    @PostMapping("/authUser/cancel")
    public AjaxResult cancelAuthUser(@RequestBody UserRole userRole)
    {
        return toAjax(roleService.deleteAuthUser(userRole));
    }
    @ApiOperation(value = "批量选择用户授权")
    @PostMapping("/authUser/selectAll")
    public AjaxResult selectAuthUserAll(@RequestBody UserRoleDto dto) {

        return toAjax(roleService.insertAuthUsers(dto.getRoleId(), dto.getUserId()));
    }

    @ApiOperation(value = "批量取消用户授权")
    @PostMapping("/authUser/cancelAll")
    public AjaxResult cancelAuthUserAll(@RequestBody UserRoleDto dto) {
        return toAjax(roleService.deleteAuthUsers(dto.getRoleId(), dto.getUserId()));
    }

    @ApiOperation(value = "更新角色状态")
    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Role role)
    {
        return toAjax(roleService.updateById(role));
    }

    @ApiOperation(value = "校验用户是否已授权")
    @PostMapping("/checkUserRole")
    public AjaxResult checkUserRole(@RequestBody BtnRoleDto dto) {

        return success(roleService.checkUserRole(dto));
    }


}
